﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluginFramework.Common;
using QLBH.StandardUI;
using QLBH.Category.Presenter;
using System.Windows.Forms;
using QLBH.Model.Controller;
using DevComponents.DotNetBar;
using QLBH.FeatureBase.Modules.Category;
using QLBH.FeatureBase;
using QLBH.Base;
using QLBH.ModelInterface;

namespace QLBH.Category
{
    [ExtensionOf(typeof(QLMatHangToolExtensionPoint))]
    public class QLMatHangTool:BaseView,IQLMatHangTool
    {
        public FormViewBase GetView(object arg)
        {
            return new QLMatHangTool();
        }
        public QLMatHangTool()
            :base()
        {

        }
        public List<IQLMatHangModel> ListMatHang;
        public override void AddNew(object sender, EventArgs e)
        {
            AddMatHangDlg add = new AddMatHangDlg();

            add.DlgType = eDlgType.ADD;
            if (add.ShowDialog()==DialogResult.OK)
            {            

                LoadDSMatHang();
            }
           
           
        }
        public override void EditItem(object sender, EventArgs e)
        {
            if (base.DataGridView.SelectedRows.Count>0)
            {
                var model = base.DataGridView.SelectedRows[0].DataBoundItem as IQLMatHangModel;
                if (model != null)
                {
                    AddMatHangDlg add = new AddMatHangDlg();
                    add.DlgType = eDlgType.EDIT;
                    add.Model = model;

                    if (add.ShowDialog()==DialogResult.OK)
                    {
                        LoadDSMatHang();
                    }
                }
            }
            
            

        }
        public override void DeleteItem(object sender, EventArgs e)
        {
            IQLMatHangModel model = base.DataGridView.SelectedRows[0].DataBoundItem as IQLMatHangModel;
            MatHangController.GetInstance().Delete(model.ToEntity());
            LoadDSMatHang();
        }
        public override void BuildSearchMenu()
        {
            ButtonItem btnSearchByID = new ButtonItem("btnSearchByID", "Tìm theo Mã hàng hóa");
            btnSearchByID.Click += (s, v) =>
            {
                List<IQLMatHangModel> listMathang = (List<IQLMatHangModel>)DataSource;
                listMathang = listMathang.Where(m => m.MHCODE.Contains(base.TbxSearchInput.ControlText)).ToList();
                base.DataGridView.DataSource=listMathang;

            };
            ButtonItem btnSearchByName = new ButtonItem("btnSearchByName", "Tìm theo Tên mặt hàng");
            btnSearchByName.Click += (s, v) => {
                List<IQLMatHangModel> listMathang = (List<IQLMatHangModel>)DataSource;
                listMathang = listMathang.Where(m => m.MHTEN.Contains(base.TbxSearchInput.ControlText)).ToList();
                base.DataGridView.DataSource = listMathang;
            };
            base.BtnSearch.SubItems.Add(btnSearchByID);
            base.BtnSearch.SubItems.Add(btnSearchByName);
           
        }
        public override void RefreshData(object sender, EventArgs e)
        {
            LoadDSMatHang();
            base.RefreshData(sender, e);
        }

        public override void LoadForm(object sender, EventArgs e)
        {
            base.LoadForm(sender, e);
            LoadDSMatHang();
        }
        private void LoadDSMatHang()
        {
            try
            {
                QLMatHangPresenter presenter=new QLMatHangPresenter();
                
                DataSource = presenter.GetDSMatHang();             
               
                DataGridView.DataSource = DataSource;

                DataGridView.Columns["MHID"].Visible = false;
                DataGridView.Columns["MHID"].DisplayIndex = 19;

                DataGridView.Columns["MHCODE"].HeaderText = "Mã MH";
                DataGridView.Columns["MHCODE"].DisplayIndex = 0;

                DataGridView.Columns["MHTEN"].HeaderText = "Tên MH";
                DataGridView.Columns["MHTEN"].DisplayIndex = 1;

                DataGridView.Columns["SOLUONG"].HeaderText = "Số Lượng";
                DataGridView.Columns["SOLUONG"].DisplayIndex = 2;

                DataGridView.Columns["GIANHAP"].HeaderText = "Giá Nhập";
                DataGridView.Columns["GIANHAP"].DisplayIndex = 3;

                DataGridView.Columns["GIABANLE"].HeaderText = "Giá Bán Lẻ";
                DataGridView.Columns["GIABANLE"].DisplayIndex = 4;

                DataGridView.Columns["GIABANBUON"].HeaderText = "Giá Bán Buôn";
                DataGridView.Columns["GIABANBUON"].DisplayIndex = 5;

                DataGridView.Columns["NGAYDCGIANHAP"].Visible = false;
                DataGridView.Columns["NGAYDCGIANHAP"].DisplayIndex = 18;

                DataGridView.Columns["NGAYDCGIABANLE"].Visible = false;
                DataGridView.Columns["NGAYDCGIABANLE"].DisplayIndex = 17;

                DataGridView.Columns["NGAYDCGIABANBUON"].Visible = false;
                DataGridView.Columns["NGAYDCGIABANBUON"].DisplayIndex = 16;

                DataGridView.Columns["CHIETKHAU"].HeaderText = "Chiết khấu";
                DataGridView.Columns["CHIETKHAU"].DisplayIndex = 6;

                DataGridView.Columns["XUATXU"].HeaderText = "Xuất Xứ";
                DataGridView.Columns["XUATXU"].DisplayIndex = 7;

                DataGridView.Columns["GHICHU"].HeaderText = "Ghi Chú";
                DataGridView.Columns["GHICHU"].DisplayIndex = 8;

                DataGridView.Columns["THEODOI"].Visible = false;
                DataGridView.Columns["THEODOI"].DisplayIndex = 15;

                DataGridView.Columns["TENNHOMHANG"].HeaderText = "Tên NH";
                DataGridView.Columns["TENNHOMHANG"].DisplayIndex = 9;

                DataGridView.Columns["TENNCC"].HeaderText = "Tên NCC";
                DataGridView.Columns["TENNCC"].DisplayIndex = 11;

                DataGridView.Columns["TENDVT"].HeaderText = "Đơn VT";
                DataGridView.Columns["TENDVT"].DisplayIndex = 10;

                DataGridView.Columns["DVTID"].Visible = false;
                DataGridView.Columns["DVTID"].DisplayIndex = 12;

                DataGridView.Columns["NCCID"].Visible = false;
                DataGridView.Columns["NCCID"].DisplayIndex = 13;

                DataGridView.Columns["NHOMHANGID"].Visible = false;
                DataGridView.Columns["NHOMHANGID"].DisplayIndex = 14;
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // QLMatHangTool
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.Name = "QLMatHangTool";
            this.Size = new System.Drawing.Size(875, 550);
            this.ResumeLayout(false);

        }
    }
}
